package com.cheboohui.iqs.deal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.common.verification.CarVerification;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.config.PrivateTool;
import com.cheboohui.iqs.controller.BaseController;
import com.cheboohui.iqs.spider.login.VerificationLogin;
import com.cheboohui.iqs.spider.pingan.BeforePolicyPingAn;
import com.cheboohui.iqs.spring.entity.price.response.PriviousResult;

/**
 * 自定义接口处理
 * 
 * @author xielonghai
 *
 */
public class CustomizeDeal {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(CustomizeDeal.class);

	/**
	 * 自定义接口处理
	 * 
	 * @param request
	 * @param response
	 */
	public static void customize(HttpServletRequest request, HttpServletResponse response) {
		log.info("进入" + Config.value + ",自定义处理接口....");
		log.error("进入" + Config.value + ",自定义处理接口....");
		log.debug("进入" + Config.value + ",自定义处理接口....");
		try {
			JSONObject param = PrivateTool.paramtenToJson(request.getParameterMap());
			if (null == param)
				BaseController.setResponseText(response, "请求参数获取转化异常");
			log.info(Config.value + ",自定义处理接口请求参数:" + param);

			String result = "暂时该接口未启用";

			if (!StringUtil.isNulls(param.getString("type"))) {
				if ("policy".equals(param.getString("type"))) {
					result = getPolicy(param.getString("licenceNo"));
				}else if ("test".equals(param.getString("type"))) {
					result=getTest();
				}
			}

			BaseController.setResponseText(response, result);
		} catch (Exception e) {
			log.error(Config.value + ",自定义处理接口实现异常:" + e);
			BaseController.setResponseText(response, "自定义处理接口实现异常:" + e.getMessage());
		}
	}
	
	private static String getTest(){
		
		
		return "测试";
	}

	/**
	 * @param licenceNo
	 *            车牌号
	 * @return
	 */
	private static String getPolicy(String licenceNo) {
		JSONObject obj = new JSONObject();
		try {
			if (!CarVerification.valLicence(licenceNo)) {
				obj.put("ok", false);
				obj.put("code", "C01");
				obj.put("msg", "车牌号格式错误");
			} else {
				PriviousResult policy = BeforePolicyPingAn.checkPolicyByLicenceNo(licenceNo, "1",
						VerificationLogin.getNo());
				if (policy.isState()) {
					obj.put("ok", true);
					JSONObject result = new JSONObject();
					JSONObject insurance = new JSONObject();
					insurance.put("bsCompanyName", "平安财险");
					insurance.put("bzPolicyNo", policy.getResult().getPolicy().getTraficInsuredNo());
					result.put("insurance", insurance);
					obj.put("result", result);
				} else {
					obj.put("ok", false);
					if (policy.getCode().equals("C03")) {
						obj.put("code", "C03");
						obj.put("msg", "无保单信息");
					} else {
						obj.put("code", "L03");
						obj.put("msg", "无法提供服务");
					}
				}
			}
		} catch (Exception e) {
			log.error(Config.value + ",自定义处理接口实现异常:" + e);
			obj.put("ok", false);
			obj.put("code", "L03");
			obj.put("msg", "无法提供服务--异常:" + e.getMessage());
		}

		return obj.toJSONString();
	}
}
